//
//  ZooLoginController.m
//  ZooNgonDoiOS
//
//  Created by Anh Jun Linh Tinh on 6/1/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "ZooLoginController.h"
#import "Constants.h"
#import "HomeViewController.h"
#import "MainViewController.h"
#import "ZooLoginScreen.h"
#import "ZooNetwork.h"

@implementation ZooLoginController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        CGRect screenSize = [[UIScreen mainScreen] bounds];
//        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenSize.size.width, screenSize.size.height) style:UITableViewStyleGrouped];
//        
//        [self setupView];
        _loginScreen = [[ZooLoginScreen alloc] initWithFrame:CGRectMake(0, 0, screenSize.size.width, screenSize.size.height) controller:self];
        
        self.tableView = _loginScreen;
//        _loginScreen.changeViewDelegate = self;
        if(![ZooNetwork sharedNetwork].isAuth){
            [[ZooNetwork sharedNetwork] sendRequestOAuth];
                //Hien thi waiting bar
        }
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(![ZooNetwork sharedNetwork].isAuth){
        [[ZooNetwork sharedNetwork] sendRequestOAuth];
            //Hien thi waiting bar
        NSLog(@"Auth is OK!");
    }
}

#pragma mark- ZooAuthDelegate

- (void)onAuthOK:(NSString *)token tokenSecret:(NSString *)token_secret {
    NSLog(@"Auth is OK!");

}

- (void)onAuthErrorWithMessage:(NSString *)mess {
    
    
}

//Setup each cell for login form.
- (void) setupCell: (UITableViewCell*) cell rowIndex: (NSInteger) index {
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
    
    switch (index) {
        case 0:
            [textField setPlaceholder:UsernameTextConstants];
            [cell addSubview:textField];
            
            break;
        case 1:
            //Password secure
            textField.secureTextEntry = YES;
            [textField setPlaceholder:PasswordTextConstants];
            [cell addSubview:textField];
            
            break;
        default:
            break;
    }
}

- (void) setupView {
    self.tableView.userInteractionEnabled = YES;
    
    UIImage *backgroundImg = [UIImage imageNamed:BackgroundImageConstants];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage: backgroundImg];
    
    [backgroundImg release];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void) customizeNavigationBar: (UINavigationController*) navigationController {
    _navigationController = navigationController;
    
    [_loginScreen customizeNavigationBar:navigationController];
}

- (void) loginSuccessed {
    
    HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    [self.navigationController pushViewController:homeViewController animated:YES];
    
//    MainViewController *mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
//    [self.navigationController pushViewController:mainViewController animated:YES];
    
    [homeViewController release];
}

- (void) setToBeanLogin: (NSString *)pUsername pass:(NSString *)pPass {
    _loginScreen.txtUsername.text = pUsername;
    _loginScreen.txtPassword.text = pPass;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
